import MsgManager from "./msg/MsgManager";
import MsgSend from "./msg/MsgSend";

import { move } from "./msg/msg"
import IScript from "./msg_scripts/IScript";
import MsgScriptManager from "./msg_scripts/MsgScriptManager";
// import { move } from "./types/msg";
// import { move as moveType } from "./types/msg";


export default class WebSocketClient {
    address: string;
    webSocket: WebSocket;
    public constructor(addr: string) {
        this.address = addr;

    }
    public start() {
        this.webSocket = new WebSocket(this.address);
        this.socketBindEvent();
        MsgSend.socket = this.webSocket
    }
    socketBindEvent() {
        this.webSocket.onopen = this.onOpen.bind(this);
        this.webSocket.onmessage = this.onMessage.bind(this);
        this.webSocket.onerror = this.onError.bind(this);
        this.webSocket.onclose = this.onClose.bind(this);
    }
    onOpen(ev: Event) {
        console.log("webSocket连接建立成功... " + "服务端address: " + this.address);
        //连接成功 发送消息

        let msg = {
            x: 1,
            y: 2,
            str: `当我们搞定整个。`
        }
        // let msg = move.C2S_SyncPosition.create()
        MsgSend.send(move.C2S_SyncPosition,msg)

        // let msgInfo = {
        //     msgId: 1,
        //     data: move.C2S_SyncPosition.encode(msg).finish()
        // }
        // // let blobBody = new Blob([msgInfo.data])
        // let allLength = 4 + 4 + msgInfo.data.length;
        // // let buffer = new ArrayBuffer(allLength);
        // let buffer = new Int8Array(8);

        // let view = new DataView(buffer.buffer)
        // view.setInt32(0, allLength);
        // view.setInt32(4, msgInfo.msgId);
        // let result = new Blob([view.buffer, msgInfo.data.buffer])

        // // this.sendData(JSON.stringify(testObj));
        // this.sendData(result);

    }
    onMessage(event: MessageEvent) {
        console.log("收到消息：", event.data);
        if (!(event.data instanceof Blob)) {
            console.log("收到非二进制数据：" + event.data);
            return
        }
        let dataBlob = event.data
        dataBlob.arrayBuffer().then(v => {

            let dataView = new DataView(v)
            let allLength = dataView.getInt32(0)
            let msgId = dataView.getInt32(4)
            let body = v.slice(8, allLength)
            if (!MsgManager.instance.getMsg(msgId)) {
                console.log("收到未定义的消息id：" + msgId);
                return
            }
            let result = MsgManager.instance.getMsg(msgId).decode(new Uint8Array(body));
            if (!MsgScriptManager.instance[msgId]) {
                console.log("未找到对应的消息处理器，消息id：" + msgId);
                return
            }
            (MsgScriptManager.instance[msgId] as IScript).excute(result)
        })
    }
    onError(ev: Event) {
        console.log(ev);

    }
    onClose(ev: CloseEvent) {
        console.log("链接关闭了");
    }
    sendData(data: string | ArrayBufferLike | Blob | ArrayBufferView) {
        this.webSocket.send(data)
    }
}